//
//  STOrderHistoryCollectionViewCell.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/27.
//

import UIKit

class STOrderAddressItemView: UIView {
    
    var type: Int
    
    override init(frame: CGRect) {
        self.type = 0
        super.init(frame: frame)
    }

    convenience init(type: Int ) {
        self.init(frame: CGRect.zero)
        self.type = type
   
        addSubview(iconView)
        iconView.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.left.equalTo(16)
            make.width.equalTo(18)
            make.height.equalTo(18)
        }
        
        addSubview(descLabel)
        descLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.left.equalTo(iconView.snp.right).offset(8)
            make.width.equalTo(68)
        }
        
        addSubview(addressLabel)
        addressLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(descLabel.snp.centerY)
            make.left.equalTo(descLabel.snp.right)
            make.right.equalTo(-16)
        }
        
        if(type == 0) {
            iconView.image = UIImage.init(named: "icon_tixiang_dizhi")
            descLabel.text = "提空箱："
            descLabel.textColor = UIColor.init(hexString:"#008CFF", alpha: 1)
            addressLabel.text = "广东省东莞市南岗区长江路366"
        } else if(type == 1) {
            iconView.image = UIImage.init(named: "icon_zhuanghuo_dizhi")
            descLabel.text = "装货："
            descLabel.textColor = UIColor.init(hexString:"#FA951E", alpha: 1)
            addressLabel.text = "鸿福路200号第一国际"
        } else {
            iconView.image = UIImage.init(named: "icon_luozhong_dizhi")
            descLabel.text = "落重："
            descLabel.textColor = UIColor.init(hexString:"#01DD9D", alpha: 1)
            addressLabel.text = "广东省东莞市南岗区长江路366"
        }
    }
    
    public func updateValue(orderType: Int, _ str: String) {
        addressLabel.text = str
        if(type == 0) {
            descLabel.text = orderType == 0 ? "提空箱：" : "提空箱"
        } else if(type == 1) {
            descLabel.text = orderType == 0 ? "装货：" : "卸货"
        } else {
            descLabel.text = orderType == 0 ? "落重：" : "还箱"
        }
    }
    
    lazy var iconView: UIImageView = {
        let imgView = UIImageView.init()
        return imgView
    }()
    
    lazy var descLabel: UILabel = {
        let lb = UILabel.init()
        lb.font = UIFont.systemFont(ofSize: 15)
        lb.text = ">"
        return lb
    }()

    lazy var addressLabel: UILabel = {
        let lb = UILabel.init()
        lb.font = UIFont.systemFont(ofSize: 16)
        lb.textColor = UIColor.init(hexString:"#323232", alpha: 1)
        lb.text = ">"
        return lb
    }()

    required init?(coder aDecoder: NSCoder) {
        self.type = 0
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }
}

class STOrderHistoryCollectionViewCell: UICollectionViewCell {
    
    class var identifier: String {
        get {
            return "STOrderHistoryCollectionViewCell"
        }
    }
    
    class func registerTo(_ collectionView: UICollectionView) {
        collectionView.register(STOrderHistoryCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: STOrderHistoryCollectionViewCell.identifier)
    }
    
    class func reusableCell(_ collectionView: UICollectionView, indexPath: IndexPath) -> STOrderHistoryCollectionViewCell? {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: STOrderHistoryCollectionViewCell.identifier, for: indexPath) as? STOrderHistoryCollectionViewCell
        return cell
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.white
        layer.cornerRadius = 12

        addSubview(orderNoLabel)
        orderNoLabel.snp.makeConstraints { (make) in
            make.top.equalTo(19)
            make.left.equalTo(17)
            make.height.equalTo(22)
        }
        
        addSubview(stateView)
        stateView.snp.makeConstraints { (make) in
            make.centerY.equalTo(orderNoLabel)
            make.right.equalTo(0)
            make.width.equalTo(80)
            make.height.equalTo(28)
        }
        
        addSubview(stateLabel)
        stateLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(orderNoLabel)
            make.right.equalTo(-15)
        }
        
        let bgView: UIView = UIView.init()
        bgView.backgroundColor = UIColor.init(hexString: "#F4F9FF", alpha: 1)
        bgView.layer.cornerRadius = 10
        addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalTo(orderNoLabel.snp.bottom).offset(15)
            make.height.equalTo(112)
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }

        bgView.addSubview(yuanView)
        yuanView.snp.makeConstraints { (make) in
            make.top.equalTo(17)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(20)
        }

        bgView.addSubview(danView)
        danView.snp.makeConstraints { (make) in
            make.top.equalTo(yuanView.snp.bottom).offset(12)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(20)
        }

        bgView.addSubview(shiView)
        shiView.snp.makeConstraints { (make) in
            make.top.equalTo(danView.snp.bottom).offset(12)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(20)
        }
        
        addSubview(bizLabel)
        bizLabel.snp.makeConstraints { (make) in
            make.top.equalTo(bgView.snp.bottom).offset(17)
            make.left.equalTo(bgView)
            make.height.equalTo(22)
        }
        
        addSubview(timeLabel)
        timeLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(bizLabel)
            make.right.equalTo(bgView)
            make.height.equalTo(22)
        }
    }
    
    public func updateData(data: Dictionary<String, Any>, type: Int, _ index: Int) {
        let orderNo = data["orderNo"] as! String
        orderNoLabel.text = "订单：".appending(orderNo)
        stateLabel.text = data["statusName"] as? String
        let createBy = data["createBy"] as! String
        bizLabel.text = "业务员：".appending(createBy)
        timeLabel.text = data["updateTime"] as? String
        yuanView.updateValue(orderType: type, data["pickUpFrom"] as? String ?? "")
        danView.updateValue(orderType: type, data["loadFrom"] as? String ?? "")
        shiView.updateValue(orderType: type, data["finishFrom"] as? String ?? "")
    }
    
    lazy var iconView: UIImageView = {
        let imgView = UIImageView.init()
        return imgView
    }()
    
    lazy var orderNoLabel: UILabel = {
        let lb = UILabel.init()
        lb.textAlignment = .left
        lb.textColor = UIColor.init(hexString: "#8F92A1", alpha: 1)
        lb.font = UIFont.systemFont(ofSize: 16)
        return lb
    }()
    
    lazy var stateView: UIView = {
        let itemView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 80, height: 28))
        itemView.backgroundColor = UIColor.init(hexString: "#F4F9FF", alpha: 1)
        itemView.setCorndersRaduis(raduis: 14, roundingCorners: [.topLeft, .bottomLeft])
        return itemView
    }()
    
    lazy var stateLabel: UILabel = {
        let lb = UILabel.init()
        lb.textAlignment = .right
        lb.textColor = UIColor.init(hexString: "#008CFF", alpha: 1)
        lb.font = UIFont.systemFont(ofSize: 16)
        return lb
    }()
    
    lazy var yuanView: STOrderAddressItemView = {
        let itemView = STOrderAddressItemView.init(type: 0)
        return itemView
    }()
   
    lazy var danView: STOrderAddressItemView = {
        let itemView = STOrderAddressItemView.init(type: 1)
        return itemView
    }()
    
    lazy var shiView: STOrderAddressItemView = {
        let itemView = STOrderAddressItemView.init(type: 2)
        return itemView
    }()
    
    lazy var bizLabel: UILabel = {
        let lb = UILabel.init()
        lb.textAlignment = .left
        lb.textColor = UIColor.init(hexString: "#323232", alpha: 1)
        lb.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.bold)
        return lb
    }()
    
    lazy var timeLabel: UILabel = {
        let lb = UILabel.init()
        lb.textAlignment = .right
        lb.textColor = UIColor.init(hexString: "#8F92A1", alpha: 1)
        lb.font = UIFont.systemFont(ofSize: 14)
        return lb
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

